package com.noahplat.user.controller;


import com.noahplat.common.utils.Result;
import com.noahplat.user.pojo.Permission;
import com.noahplat.user.service.PermissionService;
import com.noahplat.user.utils.PermissionDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

/**
 * 权限相关操作
 *
 * @author
 * @version 1.0.0
 * @Date: 2021/2/27 11:58
 **/

@RequestMapping("/api/permission")
@RestController
@Validated
public class PermissionController {
    @Autowired
    private PermissionService permissionService;

    /**
     * 分页查询权限
     * @param page
     * @param limit
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:PermissionController:getPermission')")
    @GetMapping("/getPermission")
    public Result getPermission(Long stid,@RequestParam(defaultValue = "1")Integer page, @RequestParam(defaultValue = "10")Integer limit){
        return permissionService.getPermission(stid,page, limit);
    }

    /**
     * 添加权限
     * @param permission
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:PermissionController:addPermission')")
    @PostMapping("/addPermission")
    public Result addPermission(@RequestBody Permission permission){
        return permissionService.addPermission(permission);
    }

    /**
     * 修改权限
     * @param permission
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:PermissionController:updatePermission')")
    @PutMapping("/updatePermission")
    public Result updatePermission(@RequestBody Permission permission){
        return permissionService.updatePermission(permission);
    }

    /**
     * 删除权限
     * @param id
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:PermissionController:delPermission')")
    @DeleteMapping("/delPermission")
    public Result delPermission(@NotNull(message = "ID不能为空 ") @Min(value = 1, message = "Id不能为空")Long id){
        return permissionService.delPermission(id);
    }

    /**
     * 批量添加权限
     * @param permissionDao
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:PermissionController:inserPermission')")
    @PostMapping("/inserPermission")
    public Result inserPermission(@RequestBody PermissionDao permissionDao){
        return permissionService.inserPermission(permissionDao);
    }

    /**
     * 查询权限信息
     * @return
     */
    @PreAuthorize("hasAuthority('noahplat-service-user:PermissionController:getPermissions')")
    @GetMapping("/getPermissions")
    public Result getPermissions(){
        return permissionService.getPermissions();
    }
}
